package com.xes.attend.scheduler.impl;


import com.xes.attend.config.ServerConfig;
import com.xes.attend.domain.BaseException;
import com.xes.attend.scheduler.Scheduler;
import com.xes.attend.service.AttendDataService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;


/***
 * @description 开课前刷卡时间限制参数下载
 * @author wjl
 * @ClassName: DownloadBeginClassTimeService
 * @Version 版本
 * @ModifiedBy wjl
 * @Copyright ers
 * @date 2015年12月03日 上午11:46:05
 */
@Service
public class DownloadBeginClassTimeService implements Scheduler {
    private static final Logger log = LogManager.getLogger(DownloadBeginClassTimeService.class);
    @Autowired
    private ServerConfig config;
    @Autowired
    private AttendDataService attendDataService;

    @Override
    @Scheduled(cron = "0 0 */1 * * *")        // 每小时执行一次
//    @Scheduled(fixedDelay = 1000 * 60 * 2)        // 2 minuts
    public void execute() {
        StopWatch stopWatch = new StopWatch("开课前刷卡时间限制参数下载服务VenueId：" + config.getAreaCode());
        stopWatch.start("下载");
        Integer beginClassTime = 0;
        try {
            beginClassTime = attendDataService.queryAttendTimeBegin(config.getAreaCode(), config.getBeginClassTime());
            config.setBeginClassTime(beginClassTime);
        } catch (BaseException e) {
            log.error("开课前刷卡时间限制参数下载失败:{}:{},{}", e.getResultCode(), e.getResultCode(), e.getMessage());
        }
        log.info("开课前刷卡时间限制参数下载值:{}分钟", config.getBeginClassTime());
        stopWatch.stop();
		log.debug(stopWatch.prettyPrint());
    }

}
